﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is Jutsu Opensource Framework.
  
  The Initial Developer of the Original Code is 
  ALCARAZ Marc <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2012-2013
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
  Alternatively, the contents of this file may be used under the terms of
  either the GNU General Public License Version 2 or later (the "GPL"), or
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  in which case the provisions of the GPL or the LGPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of either the GPL or the LGPL, and not to allow others to
  use your version of this file under the terms of the MPL, indicate your
  decision by deleting the provisions above and replace them with the notice
  and other provisions required by the LGPL or the GPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the MPL, the GPL or the LGPL.
  
*/

package jutsu.process.tests
{
    import jutsu.logging.logger;

    import system.process.Task;

    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Loader;
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MediaEvent;
    import flash.media.CameraUI;
    import flash.media.MediaPromise;
    import flash.media.MediaType;

    /**
     * This task test the CameraUI class.
     */
    public class CameraUITest extends Task
    {
        /**
         * Creates a new CameraUITest instance.
         */
        public function CameraUITest( target:DisplayObjectContainer = null )
        {
            this.target = target ;
        }
        
        /**
         * The camera ui singleton.
         */
        public var cameraUI:CameraUI = new CameraUI() ;
        
        /**
         * The internal loader to load the media.
         */
        public var loader:Loader = new Loader();
        
        /**
         * The owner target of this task.
         */
        public var target:DisplayObjectContainer ;
        
        /**
         * Run the process.
         */
        public override function run( ...arguments:Array ):void
        {
            notifyStarted() ;
            
            if( CameraUI.isSupported )
            {
                logger.debug( this + " run, initializing camera." );
                
                registerCameraUI() ;
                
                cameraUI.launch( MediaType.IMAGE );
                
                return ;
            }
            else
            {
                logger.debug( this + " run : initializing camera." );
            }
            
            notifyFinished() ;
        }
        
        ////////////
        
        protected function registerCameraUI():void
        {
            cameraUI.addEventListener( MediaEvent.COMPLETE , captured );
            cameraUI.addEventListener( Event.CANCEL        , canceled );
            cameraUI.addEventListener( ErrorEvent.ERROR    , error    );
        }
        
        protected function unregisterCameraUI():void
        {
            cameraUI.removeEventListener( MediaEvent.COMPLETE , captured );
            cameraUI.removeEventListener( Event.CANCEL        , canceled );
            cameraUI.removeEventListener( ErrorEvent.ERROR    , error    );
        }
        
        ////////////
        
        private function canceled( event:Event ):void
        {
            logger.warn( this + " media capture canceled." ) ;
            unregisterCameraUI() ;
            notifyFinished() ;
        }
        
        private function captured( event:MediaEvent ):void
        {
            logger.debug( this + " media captured." );
            
            unregisterCameraUI() ;
            
            var imagePromise:MediaPromise = event.data as MediaPromise ;
            
            if( imagePromise.isAsync )
            {
                logger.info( this + " asynchronous media promise." );
                
                loader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
                loader.addEventListener( IOErrorEvent.IO_ERROR, error );
               
                loader.loadFilePromise( imagePromise );
            }
            else
            {
                logger.info( this + " synchronous media promise." );
                loader.loadFilePromise( imagePromise );
                showMedia();
            }
        }
        
        private function error( error:ErrorEvent ):void
        {
            logger.error( "Error:" + error.text );
            unregisterCameraUI() ;
            notifyFinished() ;
        }
        
        private function imageLoaded( event:Event ):void
        {
            logger.debug( this + " image loaded." );
            showMedia() ;
        }
        
        private function showMedia():void
        {
            logger.debug( this + " show media." );
            if( target )
            {
                var display:DisplayObject = loader.content ;
                if( display )
                {
                    logger.info( this + " display width:" + display.width + " height:" + display.height ) ;
                    logger.info( this + " display scaleX:" + display.scaleX + " scaleY:" + display.scaleY ) ;
                    
                    display.width = 480 ;
                    display.scaleY = display.scaleX ;
                    
                    target.addChild( loader );
                }
                notifyFinished() ;
            }
        }
    }
}
